/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/3/15  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.ResponseDO;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.StoreBook;
import com.sansi.pencilbook.service.StoreBookServer;
import com.sansi.pencilbook.util.ExcelUtil;
import com.sansi.pencilbook.util.MultipartFileUtil;
import com.sansi.pencilbook.util.ResponseDOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 功能说明: 书谱书籍控制器
 *
 * @author lisy31662
 */
@Slf4j
@RestController
@RequestMapping("/api/storeBook")
public class StoreBookController extends VerifyController implements BaseController {

  @Autowired
  private StoreBookServer storeBookServer;

  @PostMapping("/v/getStoreBookByIsbn")
  public ResponseDO getStoreBookByIsbn(@RequestParam("Isbn") String Isbn) {
    log.info("获得有某一书籍的所有书铺的书籍列表");
    Result result = storeBookServer.getStoreBookByIsbn(Isbn);
    return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
  }
  @PostMapping("/v/getStoreBookByStoreAndIsbn")
  public ResponseDO getStoreBookByStoreAndIsbn(@RequestParam("isbn") String isbn, @RequestParam("storeId") int storeId) {
    Result result = storeBookServer.getStoreBookByStoreAndIsbn(storeId, isbn);
    return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
  }

  @PostMapping("/u/modifyStoreBooks")
  public ResponseDO modifyStoreBooks(@RequestBody List<StoreBook> books, HttpServletRequest request) {
    if (checkAdmin(request)) {
      Result result = storeBookServer.modifyStoreBooks(books);
      return getResponseDO(result);
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @PostMapping("/u/modifyStoreBooksByCountAndPrice")
  public ResponseDO modifyStoreBooksByCountAndPrice(@RequestParam("file") MultipartFile xlsFile, HttpServletRequest request) {
    int storeId = 0;
    if ((storeId = getStore(request)) == 0) {
      try {
        File file = MultipartFileUtil.multipartFileToFile(xlsFile);
        Result result = storeBookServer.modifyCountAndPrice(file, storeId);
        MultipartFileUtil.delteTempFile(file);
        return getResponseDO(result);
      } catch (Exception e) {
        System.out.println(e.getMessage());
        return ResponseDOUtil.setResponseDO(false, e.getMessage());
      }
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @PostMapping("/u/addStoreBook")
  public ResponseDO addStoreBook(@RequestBody StoreBook storeBook, HttpServletRequest request) {
    int storeId = 0;
    if ((storeId = getStore(request)) == 0) {
      Calendar calendar = Calendar.getInstance();
      Date date = calendar.getTime();
      String uploadDate = new SimpleDateFormat("yyyyMMdd").format(date);
      storeBook.setStoreId(storeId);
      storeBook.setTime(uploadDate);
      try {
        Result result = storeBookServer.addStoreBook(storeBook);
        return getResponseDO(result);
      } catch (Exception e) {
        System.out.println(e.getMessage());
        return ResponseDOUtil.setResponseDO(false, e.getMessage());
      }
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后在操作");
    }
  }

  @PostMapping("/u/addStoreBooksByExcel")
  public ResponseDO addStoreBooksByExcel(@RequestParam("file") MultipartFile xlsFile, HttpServletRequest request) {
    int storeId = 0;
    if (checkAdmin(request) || (storeId=getStore(request))!=0 ) {
      try {
        File file = MultipartFileUtil.multipartFileToFile(xlsFile);
        Result result = storeBookServer.addStoreBookByExcel(file,storeId);
        MultipartFileUtil.delteTempFile(file);
        return getResponseDO(result);
      } catch (Exception e) {
        log.error(e.getMessage());
        return ResponseDOUtil.setResponseDO(false, e.getMessage());
      }
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后再来");
    }
  }

  @PostMapping("/u/updateStoreBooksByExcel")
  public ResponseDO updateStoreBooksByExcel(@RequestParam("file") MultipartFile xlsFile, HttpServletRequest request) {
    int storeId = 0;
    if (checkAdmin(request) || (storeId=getStore(request))!=0 ) {
      try {
        File file = MultipartFileUtil.multipartFileToFile(xlsFile);
        Result result = storeBookServer.modifyStoreBooksByExcel(file, storeId);
        MultipartFileUtil.delteTempFile(file);
        return getResponseDO(result);
      } catch (Exception e) {
        log.error(e.getMessage());
        return ResponseDOUtil.setResponseDO(false, e.getMessage());
      }
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后再来");
    }
  }

  @PostMapping("/u/updateStoreBooksPriceByExcel")
  public ResponseDO updateStoreBooksPriceByExcel(@RequestParam("file") MultipartFile xlsFile, HttpServletRequest request) {
    int storeId = 0;
    if (checkAdmin(request) || (storeId=getStore(request))!=0 ) {
      try {
        File file = MultipartFileUtil.multipartFileToFile(xlsFile);
        Result result = storeBookServer.modifyStoreBooksPriceByExcel(file, storeId);
        MultipartFileUtil.delteTempFile(file);
        return getResponseDO(result);
      } catch (Exception e) {
        log.error(e.getMessage());
        return ResponseDOUtil.setResponseDO(false, e.getMessage());
      }
    }else {
      return ResponseDOUtil.setResponseDO(false, "请登录后再来");
    }
  }

  private ResponseDO getResponseDO(Result result)  {
    if (result.isSuccess()) {
      if (result.getEffectRow() > 0) {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getData());
      }else {
        return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg(), result.getData());
      }
    }else {
      return ResponseDOUtil.setResponseDO(result.isSuccess(), result.getMsg());
    }
  }

}
